﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Serialization;
using Batch.Amazon.ECS;
using TagCore;

namespace AmazonQuery
{
    public class AmazonResponse : IMusicData
    {
        #region Data
        Item item;
        byte[] image = null;
        private uint track;
        private string title;
        private string genre;
        private IMusicData md;
        #endregion

        #region C'tor
        public AmazonResponse(Item item, IMusicData md)
        {
            this.item = item;
            this.md = md;
            this.title = SetTitle();
            this.track = SetTrack(title);
            this.genre = SetGenre();
        }

        private uint SetTrack(string title)
        {
            string[] titles = GetTitles();
            for (int loop = 0; loop < titles.Length; loop++)
            {
                if (string.Compare(title, titles[loop]) == 0)
                {
                    return (uint)(loop + 1);
                }
            }
            return 0;
        }

        private string SetTitle()
        {
            string[] titles = GetTitles();
            return TagCoreUtil.MatchTitle(titles, md.Title);
        }

        private string[] GetTitles()
        {
            List<string> titles = new List<string>();
            if (item.Tracks != null)
            {
                if (item.Tracks.Length > 0)
                {
                    if (item.Tracks[0].Track != null)
                    {
                        foreach (TracksDiscTrack track in item.Tracks[0].Track)
                            titles.Add(track.Value.Trim());
                    }
                }
            }
            return titles.ToArray();
        }

        private string[] GetGenres()
        {
            List<string> genres = new List<string>();
            if (item.BrowseNodes != null && item.BrowseNodes.BrowseNode != null)
            {
                foreach (BrowseNode bn in item.BrowseNodes.BrowseNode)
                    genres.Add(bn.Name);
            }
            return genres.ToArray();
        }

        private string SetGenre()
        {
            string[] genres = GetGenres();
            return TagCore.Genres.Match(genres);
        }
        #endregion

        #region Public Properties
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string[] Artists
        {
            get
            {
                return item.ItemAttributes.Artist;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string[] AlbumArtists
        {
            get
            {
                return item.ItemAttributes.Artist;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Genre
        {
            get
            {
                return genre;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Comments
        {
            get
            {
                return string.Empty;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public uint Track
        {
            get
            {
                return GetTrack();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public byte[] Image
        {
            get
            {
                if (image == null)
                    image = ReadImage();

                return image;
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        #endregion

        #region Private Methods
        private uint GetTrack()
        {
            return track;
        }

        private byte[] ReadImage()
        {
            if (item.LargeImage == null || string.IsNullOrEmpty(item.LargeImage.URL))
                return null;

            string url = item.LargeImage.URL;
            HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;
            HttpWebResponse resp = wr.GetResponse() as HttpWebResponse;
            List<byte> bytes = new List<byte>();
            byte[] b = new byte[65535];
            using (Stream s = resp.GetResponseStream())
            {
                int cnt = 0;
                do
                {
                    cnt = s.Read(b, 0, b.Length);
                    for (int loop = 0; loop < cnt; loop++)
                        bytes.Add(b[loop]);
                }
                while (cnt > 0);
            }
            resp.Close();

            return bytes.ToArray();
        }
        #endregion


        public string Album
        {
            get
            {
                XmlSerializer xs = new XmlSerializer(item.GetType());
                MemoryStream ms = new MemoryStream();
                xs.Serialize(ms, item);
                ASCIIEncoding enc = new ASCIIEncoding();
                string s = enc.GetString(ms.ToArray());
                if (s.Contains("onnie"))
                {
                    string t = "test";
                }

                return item.ItemAttributes.Title;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public uint Year
        {
            get
            {
                try
                {
                    string date = item.ItemAttributes.OriginalReleaseDate;
                    if (string.IsNullOrEmpty(date))
                        date = item.ItemAttributes.ReleaseDate;

                    if (!string.IsNullOrEmpty(date))
                    {
                        DateTime dt = DateTime.Parse(date);
                        return (uint)dt.Year;
                    }
                }
                catch
                {
                }
                return 1900;
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}